//This is set for the orientation towards the X

private var hitParticles : ParticleEmitter; //spark particle for when we hit something
private var spreadZ : float = 0.0;
private var spreadY : float = 0.0;
private var currentSpread : float = 0.0;
private var bulletsLeft : int = 0;
private var nextFireTime = 0.0;
private var fUp = false;
private var rUp = false;

var spreadMax : float = .1; //spread reange. 
var range = 100.0; // range of the Raycast basically the range of the bullet as well
var bulletsPerShot= 1; //public var on the Inspector. How much bullets we want to shoot on one click.
var shotSound : AudioClip;
var shotty : Transform;
var menuHunt : GameObject;
var menuHS : GameObject;
var menuSkeet : GameObject;
var menuQuit :  GameObject;
var bulletsPerClip = 40;
var reloadTime = 0.5;
var fireRate = 0.03;
var fireRatePowerUpDuration = 10;
var fireRatePowerUpEffect = 2;
var damagePerBullet = 1;
var damagePowerUpDuration = 10;
var damagePowerUpEffect = 2;
private var health;
//var bulletText : GUIText;
var reloadSound : AudioClip;

function Start(){	
	var gameobject = GameObject.Find("Player");
	health = gameobject.GetComponent("Health");
	hitParticles = GetComponentInChildren(ParticleEmitter); //find the spark particle under Gun and set it
	if(hitParticles){
		hitParticles.emit = false; //stop emitting until we hit something
	}
	bulletsLeft = bulletsPerClip;
}
function Update () {

    if (Input.GetMouseButtonDown(0)) { //wait for mouse1
		Debug.Log("Fired"); 
		attemptShot();
	}
}

function attemptShot(){
	if (bulletsLeft ==0) return;
	if (Time.time > nextFireTime){
		//print (nextFireTime);
		nextFireTime = Time.time + fireRate;
		fireShot();
		nextFireTime+= fireRate;
		//updateBullet();
		audio.PlayOneShot(shotSound);
		shotty.animation.Play("recoil");
		//shotty.animation.Play("smoke");
	}
	
}
/* 
 * function updateBullet(){
 * 	//print (bulletsLeft);
 * 	bulletText.text = "Bullets Left " + bulletsLeft.ToString();
 * }
 */

function fireShot(){
	bulletsLeft--;
	var direction; //the direction of the ray
	var hit : RaycastHit; // container for information about what we hit
	var otherScript; //place to store the die script
	var otherObj : GameObject; //the object we hit
	for (i = 0; i < bulletsPerShot - 1; i++){ //loop for however many shots we shoot - 1 as one shot is always accurate
		calcSpread(); //find a random spread
		//Debug.Log("Spread is" + currentSpread);
		//First find the forward vector then add the random spread vectors to it.
		direction =  transform.TransformDirection(Vector3.forward) + transform.TransformDirection(0, randomY(), randomZ()); 
		//Debug.Log("Y is " + spreadY + " X is " + spreadZ);
		if (Physics.Raycast (transform.position, direction, hit, range)) { //cast our ray out using the transforms current position as a starting point. Raycast will return true or false based on a collision
				otherObj = hit.collider.gameObject; //set what we hit
				//Debug.Log("Shot at: " + otherObj.tag);
				if (otherObj.tag == "Skeet"){ //if we hit a Skeet we need to kill it
					otherScript = otherObj.GetComponent(Die);
					otherScript.Die();
				}
				else if (otherObj.tag == "Menu"){ //if we hit a Menu item, select it
				Debug.Log("Menu item shot");
					if(otherObj == menuHunt){
						Debug.Log("seleted: Go Hunt");
						Application.LoadLevel(1);
					}
					if(otherObj == menuHS){
						Debug.Log("seleted: High Scores");
						Application.LoadLevel(3);
					}
					if(otherObj == menuSkeet){
						Debug.Log("seleted: Aww SkeetSkeet");
						Application.LoadLevel(2);
					}
					if(otherObj == menuQuit){
						Debug.Log("seleted: Quit");
						Application.Quit();
					}
					//otherScript = otherObj.GetComponent(gameMenu);
					//otherScript.menuShot();
				}
				else if (otherObj.tag == "Animal"){
					otherScript = otherObj.GetComponent(animalHealth);
					otherScript.hit(damagePerBullet);
					skeetsScore.Score++;
					Debug.Log("Score!");
				}
				
				if (hitParticles) { //create a spark at anything we hit
				hitParticles.transform.position = hit.point;
				//Debug.Log("Hit Location Spread" + hit.point);
				hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
				hitParticles.Emit();
				}
		}
	}
	direction =  transform.TransformDirection(Vector3.forward); //find forward again for our accurate shot
	if (Physics.Raycast (transform.position, direction, hit, range)) {
				otherObj= hit.collider.gameObject;
				//Debug.Log("Shot at: " + otherObj.tag);
				if (otherObj.tag == "Skeet"){ //if we hit a Skeet we need to kill it
					otherScript = otherObj.GetComponent(Die);
					otherScript.Die();
				}
				else if (otherObj.tag == "Menu"){ //if we hit a Menu item, select it
				Debug.Log("Menu item shot");
					if(otherObj == menuHunt){
						Debug.Log("seleted: Go Hunt");
					}
					if(otherObj == menuHS){
						Debug.Log("seleted: High Scores");
						Application.LoadLevel(2);
					}
					if(otherObj == menuSkeet){
						Debug.Log("seleted: Aww SkeetSkeet");
						Application.LoadLevel(1);
					}
					if(otherObj == menuQuit){
						Debug.Log("seleted: Quit");
						Application.Quit();
					}
					//otherScript = otherObj.GetComponent(gameMenu);
					//otherScript.menuShot();
				}
				else if (otherObj.tag == "Animal"){
					otherScript = otherObj.GetComponent(animalHealth);
					otherScript.hit(damagePerBullet);
				}
				
				else if (otherObj.tag == "fireRatePowerUp"){
					Destroy(otherObj);
					fireRatePowerUp();
				}
				
				else if (otherObj.tag == "damagePowerUp"){
					Destroy(otherObj);
					damagePowerUp();
				}
				
				else if (otherObj.tag == "healthPowerUp"){
					Destroy(otherObj);
					health.healthPowerUp();
				}
		}
		
	if (bulletsLeft == 0){
		reload();
	}
}

function calcSpread(){
	
	currentSpread = Random.Range(0.0, spreadMax); //find a random spread
	currentSpread = Mathf.Clamp (currentSpread, 0.0, spreadMax); //making sure its within the boundry

}

function randomZ(){
	spreadZ = Random.Range(-currentSpread, currentSpread); //find a random spread within the spread
	return spreadZ;
}

function randomY(){
	spreadY = Random.Range(-currentSpread, currentSpread);
	return spreadY;
}

function reload(){
	audio.PlayOneShot(reloadSound);
	yield WaitForSeconds (reloadTime);
	bulletsLeft = bulletsPerClip;
}

function GetBullets(){
	return bulletsLeft;
}

function fireRatePowerUp(){
	if ( !fUp){
	fireRate = fireRate / fireRatePowerUpEffect;
	rUp = true;
	}
	else return;
	yield WaitForSeconds(fireRatePowerUpDuration);
	fireRate = fireRate * fireRatePowerUpEffect;
	rUp = false;
}

function damagePowerUp(){
	if (!fUp){
	damagePerBullet = damagePerBullet * damagePowerUpEffect;
	fUp = true;
	}
	else return;
	yield WaitForSeconds(damagePowerUpDuration);
	damagePerBullet = damagePerBullet / damagePowerUpEffect;
	fUp = false;
}